#!/usr/bin/python

# Copyright (c) 2010-, WangFei, Cloud Team, CGCL,
# All rights reserved.
# This Program launch a job to a mpi environment.
# python lanuch master_ip master_ip_root_password "mpi_job_execute_argv"
# python launch 192.168.226.167 passw0rd "mpirun -machinefile /home/cgcl/all_vm_ip_list -np 2 /home/cgcl/cpi"
import os, sys
import paramiko

def launch_to_master(master_address,user,password,command,ssh_port):
	client = paramiko.SSHClient()
	client.load_system_host_keys()
	client.connect((master_address), username=(user), password=(password), port=(ssh_port))
	print "Jobs is launching...."
	stdin,stdout,stderr = client.exec_command(command)
	print stdout.read()
	client.close()

def main():
    cnt_argv = len(sys.argv)
    if cnt_argv != 4:
        print "Command format error. Please Using As follow:\n"
        print "      python lanuch master_ip master_ip_root_password mpi_job_execute_argv"
        sys.exit(0)
    username="root"
    ssh_port=22
    master_address = sys.argv[1]
    password = sys.argv[2]
    command = sys.argv[3]
    print "Launch Jobs to master: " + command
    launch_to_master(master_address,username,password,command,ssh_port)

if __name__ == "__main__":
    main()
